package com.example.fengyuwuzu2.utils;

import android.content.Context;
import android.content.SharedPreferences;
import com.example.fengyuwuzu2.app.App;

import java.util.HashMap;
import java.util.Map;

public class SharedPreUtils {
    private static volatile SharedPreUtils sharedPreUtils;
    private final SharedPreferences sharedPreferences;

    public SharedPreUtils() {
        sharedPreferences = App.getContext().getSharedPreferences("IReader_pref",
                Context.MODE_PRIVATE);
    }
    public static SharedPreUtils getInstance() {
        if (sharedPreUtils == null) {
            synchronized (SharedPreUtils.class) {
                if (sharedPreUtils == null) {
                    sharedPreUtils = new SharedPreUtils();
                }
            }
        }
        return sharedPreUtils;
    }

    public boolean getBoolean(String key, boolean def) {
        return sharedPreferences.getBoolean(key, def);
    }

    public void putBoolean(String key, boolean value) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(key, value);
        editor.apply();
    }

    public Map<String, String> getUserInfo() {
        Map<String, String> data = new HashMap<String, String>();
        data.put("uname", sharedPreferences.getString("uname", ""));
        data.put("pwd", sharedPreferences.getString("pwd", ""));
        return data;
    }

    public String getUname() {
        return sharedPreferences.getString("uname", "");
    }

    public void saveUserInfo(String username, String passwd) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("uname", username);
        editor.putString("pwd", passwd);
        editor.apply();
    }

}
